package com.jiaming.employee.emp.controller;

import com.jiaming.employee.common.bean.vo.MyPageVO;
import com.jiaming.employee.common.exception.ApiException;
import com.jiaming.employee.common.response.ResultCodeEnum;
import com.jiaming.employee.common.response.ResultVO;
import com.jiaming.employee.emp.bean.vo.AddEmpVO;
import com.jiaming.employee.emp.bean.vo.PageEmpDataVO;
import com.jiaming.employee.emp.bean.vo.PageEmpVO;
import com.jiaming.employee.emp.service.IEmpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author sun
 * @create 2021-05-14-21:10
 */
@Api(value = "员工API接口文档")
@RestController
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    IEmpService empService;

    @ApiOperation(value = "分页查询员工信息",notes = "接口描述")
    @PostMapping("/page_emp")
    public ResultVO<MyPageVO<PageEmpDataVO>> pageEmp(@RequestBody PageEmpVO pageEmpVO){

        if (pageEmpVO.getPageNum() == null || pageEmpVO.getPageNum() <= 0) {
            pageEmpVO.setPageNum(1);
        }
        if (pageEmpVO.getPageSize() == null || pageEmpVO.getPageSize() < 5) {
            pageEmpVO.setPageSize(5);
        }

        MyPageVO<PageEmpDataVO> list = empService.pageEmp(pageEmpVO);

        return new ResultVO<>(ResultCodeEnum.SUCCESS,list);
    }

    @ApiOperation(value = "新增员工信息",notes = "接口描述")
    @PostMapping("/add_emp")
    public ResultVO addEmp(@RequestBody @Valid AddEmpVO addEmpVO){

        empService.addEmp(addEmpVO);

        return new ResultVO(ResultCodeEnum.SUCCESS);
    }

    @ApiOperation(value = "修改员工实习状态",notes = "接口描述")
    @PostMapping("/update_status")
    public ResultVO updateStatus(@RequestParam("emp_id") Integer empId){

        empService.updateStatus(empId);

        return new ResultVO(ResultCodeEnum.SUCCESS);
    }

    @ApiOperation(value = "修改员工部门",notes = "接口描述")
    @PostMapping("/update_Dep")
    public ResultVO updateDep(@RequestParam("emp_id") Integer empId,@RequestParam("dep_id") Integer depId){

        empService.updateDep(empId,depId);

        return new ResultVO(ResultCodeEnum.SUCCESS);
    }

    @ApiOperation(value = "删除员工信息",notes = "接口描述")
    @PostMapping("/remove_Dep")
    public ResultVO removeDep(@RequestParam List<Integer> ids){
        if (ids.size() <= 0) {
            throw new ApiException("缺少必填参数");
        }
        empService.removeDep(ids);
        return new ResultVO(ResultCodeEnum.SUCCESS);
    }
}
